package com.marketlive.system.encryption;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.marketlive.system.encryption.IMaskNumber;

/**
 * Implements a credit card number masker which replaces all but the last four digits with a "*".
 */
public class MaskNumber implements IMaskNumber {
    private static Log log = LogFactory.getLog(MaskNumber.class);

    /**
     * Masks a credit card number string replacing all but the last 4 digits with with a "*".
     * The credit card number will unencrypted when it is passed to this method
     * @param creditCardNumber  the credit card number string to mask.
     * @return  returns a string which masks the number.
     */
    public String mask(final String creditCardNumber) {

        if (creditCardNumber != null && creditCardNumber.trim().length() > 0) {
            int maskLength = creditCardNumber.substring(0, creditCardNumber.length() - 4).length();
            StringBuffer maskString = new StringBuffer();
            for (int i = 0; i < maskLength; i++) {
                maskString.append("*");
            }
            StringBuffer maskedNumber = new StringBuffer(creditCardNumber);
            if (log.isDebugEnabled()) {
            	log.debug(maskedNumber.replace(0, creditCardNumber.length() - 4, maskString.toString()).toString());
            }
            return maskedNumber.replace(0, creditCardNumber.length() - 4, maskString.toString()).toString();
        }

        return creditCardNumber;
    }
}
